﻿@using EasyFramework.Core.Cache;
@using EasyCourse.Model;
@model VDiscussionInfo
@{
    Layout = null;
    bool isShowEdit = false;
    string currentUserId = CurrentUser.UserId;
    if (!string.IsNullOrEmpty(currentUserId) && currentUserId == Model.Entity.CreateId)
    {
        isShowEdit = true;
    }

    CommonPageInfo pageInfo = ViewData[CommonViewDataKey.CommonPageInfo] as CommonPageInfo;
    ViewData["CourseId"] = pageInfo.CourseId;
}
@{ViewData["MenuIndex"] = 11;}

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>讨论区</title>
    @Html.Partial("~/Views/Share/PublicHeader.cshtml")
</head>
<body>
    @Html.Partial("~/Views/Share/PublicTopNav.cshtml")
    <div class="container">
        <div class="row">
            <h1 class="topTitle-h1">
                @pageInfo.CourseName
            <br>
            <small>广州中医药大学康复治疗微课程网络学习平台</small>
        </h1>
    </div>
</div>
@Html.Partial("~/Views/Share/PublicNav.cshtml", this.ViewData)
<div class="container wrap mb10">
    <div class="row">
        <div class="col-xs-9 publicLBox3">
            <div class="lBox part">
                <div class="lBox-th">
                    <h3>讨论区</h3>
                </div>
                <div class="lBox-tb">
                    <table class="table table-bordered tlqny">
                        <thead>
                            <tr>
                                <th width="18%" class="td1">
                                    查看：<span class="orange">@Model.Entity.VisitTimes</span>
                                    回复：<span class="orange">@Model.Entity.ReplyTimes</span>
                                </th>
                                <th width="82%" class="td2">
                                    <h1>@Model.Entity.Title</h1>

                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr id="Jzt">
                                <td class="td1">
                                    <div class="userInfo">
                                        <p class="pName">
                                            @Model.Entity.Creator
                                        </p>
                                        <p class="pPic">
                                            <img src="@WebSiteDomain.ResFileDomain@Model.Portrait" width="120" height="120" alt="">
                                        </p>
                                        <p class="pInt">
                                            已发帖：@Model.UserDiscussCount
                                        </p>
                                    </div>
                                </td>
                                <td class="td2">
                                    <div class="dReplyTxt">
                                        @Html.Raw(Model.Entity.DiscussionContent)
                                    </div>
                                    <div class="dz text-right">
                                        @if (isShowEdit)
{
    <a href="@Url.Action("DiscussionInfoEdit", "Discussion")?DiscussId=@Model.Entity.Id&CourseId=@pageInfo.CourseId" target="_blank" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-pencil"></span>编辑</a>
}

                                        <button type="button" id="JdzTxt" class="btn btn-default btn-sm" disabled="disabled">@Model.Entity.Zan</button>
                                        @if (!isShowEdit)
                                        {
                                            <button type="button" id="JdzBtn" class="btn btn-warning btn-sm">
                                                <span class="glyphicon glyphicon-thumbs-up"></span>点赞
                                            </button>
                                        }

                                    </div>
                                </td>
                            </tr>

                        </tbody>
                        <tfoot id="JListContent">
                        </tfoot>
                    </table>
                    <div class="row clearfix well d-page">
                        <div id="PagInfo" class="pull-left sum"></div>
                        <div class="clearfix pull-right">
                            <ul id="PageTool" class="pagination"></ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-3">
            @Html.Partial("~/Views/Share/PublicRBox.cshtml", ViewData)
            <div class="rBox part rBox-tools mt15">
                <a class="btn btn-success btn-lg btn-block" href="" id="JaddPostsBtn" target="_blank">发布新帖</a>
                <button type="button" class="btn btn-info btn-lg btn-block btn-reply">回&nbsp;&nbsp;&nbsp;&nbsp;复</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade bs-example-modal-lg" tabindex="-1" id="Jreply" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content reply-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">Close</span>
                </button>
                <h4 class="modal-title">回复</h4>
            </div>
            <div class="modal-body">
                <p class="pTit">
                    RE: <em id="JemTit"></em>
                </p>
                <p class="pTxt" id="JpTxt"></p>
                <p class="mb10">
                    <textarea class="form-control" id="JreplyContent" rows="5"></textarea>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" id="JreplyBtn" class="btn btn-info" data-dismiss="modal">提 交</button>
            </div>
        </div>
    </div>
</div>
@Html.Partial("~/Views/Share/PublicFooter.cshtml")
@Html.Partial("~/Views/Share/PublicCommonJs.cshtml")
<script>
    // 侧栏固定
    var a = $('.rBox-tools'),
        b = $(".topNavPar");
    a.affix({
        offset: {
            top: function () {
                var c = a.offset().top, d = b.height();
                return this.top = c - d
            },
            bottom: function () {
                return (this.bottom = $('.footer').outerHeight(true))
            }
        }
    })


    // 回复弹框
    var JemTit = $("#JemTit"),
        Jptxt = $("#JpTxt"),
        Jzt = $("#Jzt"),
        JreplyContent = $("#JreplyContent");


    var JListContent = $("#JListContent"),
        DiscussId = $.getUrlParam("DiscussId"),
        CourseId = $.getUrlParam("CourseId"),
        PageNO = 1,
        PageSize = 10,
        _state = false; //第一次调用getList的时候不用回到顶部

    //发布新帖链接
    $("#JaddPostsBtn").attr("href", "@Url.Action("DiscussionInfoAdd", "Discussion")?CourseId=" + CourseId);
    
    var replyAuthor = false;//全局变量 false代码回复的是其他人，true代表回复作者
    $(window).bind("click", function (e) {
        var _self = $(e.target);
        if (_self.hasClass("btn-reply") || _self.parent().hasClass("btn-reply")) {
            var self = _self.hasClass("btn-reply") ? _self : _self.parent();
            var _tr = self.parents("tr");
            if (_tr.length > 0) {
                var _name = $(".pName", _tr).text();
                JemTit.text(_name);
                var dTxtHtml = $.trim($(".dReplyTxt", _tr).html())
                
                Jptxt.attr("data-html", dTxtHtml);
                replyAuthor = false;
            } else {
                var _name = $(".pName", Jzt).text();
                JemTit.text(_name);
                var dTxtHtml = $.trim($(".dReplyTxt", _tr).html())
               

                Jptxt.attr("data-html", dTxtHtml);
                replyAuthor = true;
            }
            $('#Jreply').modal("show")
        }
    })

    $("#JreplyBtn").bind("click", function () {
        var ReplyContent = "";
        if (!replyAuthor) {
            ReplyContent = "<div class='lgray f12 replyDiv'>" + Jptxt.attr("data-html") + "<br>" + $(".pTit", $('#Jreply')).text().trim() + "</div>";
        }
        ReplyContent += JreplyContent.val();
        $.easyPost("@Url.Action("AddComment", "Discussion")", {
            "DiscussId": DiscussId,
            "Content": ReplyContent
        }, function (data) {
            if (data.result) {
                showTips.open("回复成功！");
                $('#Jreply').modal("hide");
                JreplyContent.val("");
                getList(PageNO, DiscussId)
            } else {
                showTips.open(data.message)
            }
        }, "json")
    })
    function getList(pageNo, discussId) {
        PageNO = pageNo;
        DiscussId = discussId;
        $.easyPost("@Url.Action("GetCommentList", "Discussion")", {
            "PageNO": PageNO,
            "PageSize": PageSize,
            "DiscussId": DiscussId
        }, function (data) {
            if (!data.result) {
                showTips.open(data.message);
                return;
            }
            if (data.rows.length > 0) {
                var _row = data.rows,
                    _rowL = _row.length,
                    _html = '';
                options = {
                    totalPages: data.totalPage,
                    total: data.total,
                    currentPage: PageNO,
                    numberOfPages: PageSize,
                    onPageClicked: function (event, originalEvent, type, page) {
                        getList(page, DiscussId)
                    }
                }
                initialPaginator(options, "PageTool", "PagInfo");
                for (var i = 0; i < _rowL; i++) {
                    var item = _row[i];
                    _html += '<tr>' +
                                '<td class="td1">' +
                                    '<div class="userInfo">' +
                                        '<p class="pName">' + item.Entity.Creator + '</p>' +
                                        '<p class="pPic">' +
                                            '<img src="@WebSiteDomain.ResFileDomain' + item.UserPortrail + '" width="120" height="120" alt="">' +
                                        '</p>' +
                                        '<p class="pInt">' +
                                            '已发帖：' + item.ReplyerDiscussCount +
                                        '</p>' +
                                    '</div>' +
                                '</td>' +
                                '<td class="td2">' +
                                    '<div class="dReplyTxt">' + item.Entity.CommentContent + '</div>' +
                                    '<div class="dz text-right">' +
                                        '<button type="button" class="btn btn-info btn-sm btn-reply">' +
                                            '<span class="glyphicon glyphicon-comment"></span> 回复' +
                                        '</button>' +
                                    '</div>' +
                                '</td>' +
                            '</tr>';
                }
                JListContent.html(_html);
                if (_state) {
                    $('html,body').animate({ scrollTop: 0 }, 0);
                }
                _state = true;

            } else {
                var _html = '<tr><td colspan="2" align="center">暂无内容</td></tr>'
                JListContent.html(_html)
            }
        }, "json");
    }
    getList(PageNO, DiscussId);
    //点赞
    $("#JdzBtn").bind("click", function () {
        $.easyPost("@Url.Action("AddZan", "Discussion")", {
            "DiscussId": DiscussId
        }, function (data) {
            if (data.result) {
                showTips.open("点赞成功！");
                $('#JdzTxt').html(data.entity);
            } else {
                showTips.open(data.message)
            }
        }, "json")
    })
</script>
</body>
</html>
